package com.suixun.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据库获取到的List<Map>结果集转化为JavaBean工具类
 */
public class BeanUtil<T> {

    /**
     * 根据List<Map<String, Object>>数据转换为JavaBean数据
     * 
     * @param datas
     * @param beanClass
     * @return
     * @throws BeanConverterException
     */
    public List<T> ListMap2JavaBean(List<Map<String, Object>> datas, Class<T> beanClass)
            throws BeanConverterException {
    	 // 返回数据集合
        List<T> list = null;
        // 对象字段名称
        String fieldname = "";
        // 对象方法名称
        String methodname = "";
        // 对象方法需要赋的值
        Object methodsetvalue = "";
        try {
            list = new ArrayList<T>();
            // 得到对象所有字段
            Field fields[] = beanClass.getDeclaredFields();
            // 遍历数据
            for (Map<String, Object> mapdata : datas) {
                // 创建一个泛型类型实例
                T t = beanClass.newInstance();
                // 遍历所有字段，对应配置好的字段并赋值
                for (Field field : fields) {
                    // 获取字段名称
                    fieldname = field.getName();
                    if("serialVersionUID".equals(fieldname)){
                    	continue;
                    }
                    // 拼接set方法
                    methodname = "set" + StringUtil.capitalize(fieldname);
                    // 获取data里的对应值
                    methodsetvalue = mapdata.get(fieldname);
                    // 赋值给字段
                    Method m = beanClass.getDeclaredMethod(methodname, field.getType());
                    if (field.getType() == Short.class) {
                        if(methodsetvalue == null)
                            m.invoke(t, null);
                        else
                            m.invoke(t, Short.parseShort(methodsetvalue.toString()));
                    }else if (field.getType() == Byte.class) {
                        if(methodsetvalue == null)
                            m.invoke(t,null);
                        else
                            m.invoke(t, Byte.parseByte(methodsetvalue.toString()));
                    }else if (field.getType() == Boolean.class) {
                        if(methodsetvalue == null)
                            m.invoke(t,Boolean.FALSE);
                        else
                            m.invoke(t, Boolean.parseBoolean(methodsetvalue.toString()));
                    } else if (field.getType() == Integer.class) {
                    	if(methodsetvalue == null){
                    	}else {
                            m.invoke(t, Integer.parseInt(methodsetvalue.toString()));
                    	}
                    }else {
                        m.invoke(t, methodsetvalue);
                    }
                }
                // 存入返回列表
                list.add(t);
            }
        } catch (InstantiationException e) {
            throw new BeanConverterException(e, "创建beanClass实例异常");
        } catch (IllegalAccessException e) {
            throw new BeanConverterException(e, "创建beanClass实例异常");
        } catch (SecurityException e) {
            throw new BeanConverterException(e, "获取[" + fieldname + "] getter setter 方法异常");
        } catch (NoSuchMethodException e) {
            throw new BeanConverterException(e, "获取[" + fieldname + "] getter setter 方法异常");
        } catch (IllegalArgumentException e) {
            throw new BeanConverterException(e, "[" + methodname + "] 方法赋值异常");
        } catch (InvocationTargetException e) {
            throw new BeanConverterException(e, "[" + methodname + "] 方法赋值异常");
        }
        // 返回
        return list;
    }

}
